/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package restaurante.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import restaurante.bancodados.CriaConexao;
import restaurante.logica.Ingrediente;

/**
 *
 * @author Fernanda
 */
public class IngredienteDao {
    private Connection conexao;

    public IngredienteDao() throws SQLException{
        this.conexao = CriaConexao.getConexao();
    }

    public void adiciona(Ingrediente i1) throws SQLException{
        String  sql = "insert into ingrediente (nome, marca, estoque)" +
                "values (?,?,?)";
        PreparedStatement stmt = conexao.prepareStatement(sql);

        // Seta os valores
        stmt.setString(1, i1.getNome());
        stmt.setString(2, i1.getMarca());
        stmt.setString(3, String.valueOf(i1.getEstoque()));

        // Executa o código SQL
        stmt.execute();
        stmt.close();
    }


        public  List<Ingrediente> getLista(String busca) throws SQLException{
        String sql = "Select * from ingrediente where nome like ?";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);

        stmt.setString(1, busca);       // inserção do caracter de busca.

        ResultSet rs = stmt.executeQuery();

        List<Ingrediente> minhaLista = new ArrayList<Ingrediente>();

        while(rs.next()){
            Ingrediente i1 = new Ingrediente();
            i1.setId(rs.getInt("id"));
            i1.setNome(rs.getString("nome"));
            i1.setMarca(rs.getString("marca"));
            i1.setEstoque(rs.getInt("estoque"));

            minhaLista.add(i1);
        }

        rs.close();
        stmt.close();
        return minhaLista;
    }


    public void altera(Ingrediente i1) throws SQLException{
        String sql = "update ingrediente set nome=?, marca=?, estoque=?" +
                     "where id=?";
        PreparedStatement stmt = conexao.prepareStatement(sql);

        // Seta os valores

        stmt.setString(1, i1.getNome());
        stmt.setString(2, i1.getMarca());
        stmt.setString(3, String.valueOf(i1.getEstoque()));
        stmt.setString(4, String.valueOf(i1.getId()));

        // Executa o código SQL
        stmt.execute();
        stmt.close();

    }

    public void remove(Ingrediente i1) throws SQLException{
        String sql = "delete from ingrediente where id=?";
        PreparedStatement stmt = conexao.prepareStatement(sql);

        stmt.setString(1, String.valueOf(i1.getId()));

        stmt.execute();
        stmt.close();

    }


}
